package com.y2.authprj.mvc.controller.auth;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdglc.spring.biz.auth.SysroleBiz;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.bean.auth.vo.SysroleVo;
import com.y2.authprj.exception.UpdateException;

@Controller
@RequestMapping("/auth/sysrole")
public class SysroleController {
	
	@Resource(name="SysroleBiz")
	private SysroleBiz biz;
	public SysroleBiz getBiz() {
		return biz;
	}
	public void setBiz(SysroleBiz biz) {
		this.biz = biz;
	}
	//显示所有角色信息
	@RequestMapping("/show")
	public String show(Model model){
		try {
			List<SysroleInfo> sysroleList = biz.findAll();
			model.addAttribute("sysroleList", sysroleList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/auth/sysrole/Sysrole";
	}
	@RequestMapping("/list")
	public String list(SysroleVo vo,Model model){
		try {
			List<SysroleInfo> sysroleList = biz.findAllByName(vo);
			model.addAttribute("sysroleInfos", sysroleList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/auth/sysrole/Sysrole";
	}
	@RequestMapping("/detail")
	public String detail(Integer id,Model model){
		try {
			SysroleInfo sysroleInfo = biz.findByid(id);
			model.addAttribute("sysroleInfo", sysroleInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "auth/sysrole/jsp/detail";
	}
	
	@RequestMapping("/update")
	public String update(SysroleInfo info,Model model){
		try {
			biz.updateSysrole(info);
		}catch(UpdateException e){
			model.addAttribute("errorMessage", e.getMessage());
			return "auth/user/detail";
		} catch (Exception e) {
			e.printStackTrace();
			//return "auth/user/detail";
		}
		return "redirect:/auth/sysrole/page";
	}
	
	@RequestMapping("/showadd")
	public String showAdd(){
		
		return "auth/sysrole/jsp/add";
	}
	
	
	@RequestMapping("/add")
	public String add(SysroleInfo info,String test){
		SysroleVo vo = new SysroleVo();
		info.setMangerroleFlag(1);
		info.setDeleteFlag(1);
		try {
			biz.add(info);
		}catch(UpdateException e){

		}catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/auth/sysrole/page";
	}
	@RequestMapping("/showroleauth")
	public String showroleauth(String name,Model model){
		model.addAttribute("name", name);
		return "auth/sysrole/jsp/roleauth";
	}
	@RequestMapping("/role")
	public String role(Model model){
		return "/auth/sysrole/Sysrole";
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String[] delitems,Model model){
		for (int i = 0; i < delitems.length; i++) {
			try {
				biz.deleteSysrole(Integer.parseInt(delitems[i]));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "";
	}
	
	@RequestMapping("/page")
	public String page(Model model,@RequestParam(
			required=false,defaultValue = "1")int pageNum,
			SysroleInfo sysroleInfo){
		PageInfo<SysroleInfo> queryByPage = biz.queryByPage(sysroleInfo, pageNum, 5);
		
		List<SysroleInfo> sysroleInfos = queryByPage.getList();
		
		System.out.println(queryByPage);
		System.out.println(sysroleInfos);
		model.addAttribute("queryByPage",queryByPage);
		model.addAttribute("sysroleInfos",sysroleInfos);
		return "/auth/sysrole/Sysrole";
	}

	
}
